Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
The exec-sh npm package is a simple utility for executing shell commands in Node.js. It provides a straightforward API to run shell commands synchronously or asynchronously, with options to handle output and errors.
Execute a command asynchronously
This feature allows you to execute a shell command asynchronously. The callback function handles any errors and provides the exit code if the command fails.
const execSh = require('exec-sh');
execSh('echo Hello World', function(err){
if (err) {
console.log('Exit code:', err.code);
return;
}
console.log('Command executed successfully');
});
Execute a command synchronously
This feature allows you to execute a shell command synchronously. If the command fails, an error is thrown, and you can catch it to handle the exit code.
const execSh = require('exec-sh');
try {
execSh.sync('echo Hello World');
console.log('Command executed successfully');
} catch (err) {
console.log('Exit code:', err.code);
}
Capture command output
This feature allows you to capture the standard output and standard error of the executed command. The callback function provides the stdout and stderr as arguments.
const execSh = require('exec-sh');
execSh('echo Hello World', true, function(err, stdout, stderr){
if (err) {
console.log('Exit code:', err.code);
return;
}
console.log('stdout:', stdout);
console.log('stderr:', stderr);
});
The child_process module is a built-in Node.js module that provides the ability to spawn child processes. It offers more control and flexibility compared to exec-sh, including options to spawn, fork, exec, and execFile processes.
ShellJS is a portable (Windows/Linux/macOS) implementation of Unix shell commands on top of Node.js. It provides a more extensive set of shell commands and utilities compared to exec-sh, making it suitable for more complex scripting tasks.
Execa is a modern alternative to child_process. It provides a simpler and more powerful API for executing shell commands, with better support for promises and improved error handling. It is more feature-rich compared to exec-sh.
Execute shell command forwarding all stdio streams.
exec-sh is a wrapper for child_process.spawn
with some improvements:
cmd /C COMMAND
sh -c COMMAND
execSh("bash")
execsh("echo -n Say: && read i && echo Said:$i")
execSh("pwd", console.log)
// JavaScript
execSh("echo hello exec-sh && bash", { cwd: "/home" }, function(err){
if (err) {
console.log("Exit code: ", err.code);
}
});
# Terminal output: interactive bash session
hello exec-sh
bash-3.2$ pwd
/home
bash-3.2$ exit 99
exit
Exit code: 99
const execSh = require("../");
// run interactive bash shell
execSh("echo lorem && bash", { cwd: "/home" }, (err) => {
if (err) {
console.log("Exit code: ", err.code);
return;
}
// collect streams output
const child = execSh(["bash -c id", "echo lorem >&2"], true,
(err, stdout, stderr) => {
console.log("error: ", err);
console.log("stdout: ", stdout);
console.log("stderr: ", stderr);
});
});
const execShPromise = require("exec-sh").promise;
// run interactive bash shell
const run = async () => {
let out;
try {
out = await execShPromise('pwd', true);
} catch (e) {
console.log('Error: ', e);
console.log('Stderr: ', e.stderr);
console.log('Stdout: ', e.stdout);
return e;
}
console.log('out: ', out.stdout, out.stderr);
}
run();
execSh(command, [options], [callback])
Execute shell command forwarding all stdio.
Parameters:
command {String|Array}
- The command to run, or array of commands[options] {Object|TRUE}
- Options object passed directly to child_process.spawn
, when TRUE
then { stdio: null }
used[callback] {Function}
- callback(err, stdout, stderr)
err {Error|NULL}
- Error object. Has code
property containing last command exit code when availablestdout {String|NULL}
- aggregated stdout or NULL
if not availablestderr {String|NULL}
- aggregated stderr or NULL
if not availableReturn Values:
Returns ChildProcess object.
Complete API Documentation including private and public methods is generated from source code by JSDoc tool and is available here.
Code coverage report for all files is available here.
npm test
- run testsnpm run jsdoc
- build jsdocnpm run dev
- run tests continuouslyThe MIT License (MIT)
FAQs
Execute shell command forwarding all stdio.
We found that exec-sh demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.